﻿using System.IO;

namespace WardenMonitor
{

    /// <summary>
    /// Logs packets in a simple binary format
    /// </summary>
    public class PacketLogger
    {
        private readonly BinaryWriter _streamWriter;
        private readonly object _writerLock = new object();

        public PacketLogger(PacketHandler handler, string path)
        {
            handler.OnIncomingPacket += OnPacket;
            handler.OnOutgoingPacket += OnPacket;
            _streamWriter = new BinaryWriter(new FileStream(path, FileMode.Create));
        }

        private void OnPacket(object sender, PacketEvent e)
        {
            lock (_writerLock)
            {
                _streamWriter.Write(e.Packet.Data.Length);
                _streamWriter.Write((byte)e.Packet.Direction);
                _streamWriter.Write(e.Packet.Data);
            }
        }

    }

}